﻿using System;
using Microsoft.Win32.SafeHandles;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace PPJoy_dotNET
{
    public class csHandle
    {
        #region DLL Imports & Misc (CreateFile,CloseHandle,DevIoControl,SetupCopyOEMInf)
        [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern SafeFileHandle CreateFile(
            string fileName,
            [MarshalAs(UnmanagedType.U4)] FileAccess fileAccess,
            [MarshalAs(UnmanagedType.U4)] FileShare fileShare,
            int securityAttributes,
            [MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
            int flags,
            IntPtr template);
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern int CloseHandle(SafeFileHandle hObject);

        [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Auto)]
        public static extern bool DeviceIoControl(
            SafeFileHandle hDevice, int dwIoControlCode,
            IntPtr lpInBuffer, int nInBufferSize,
            IntPtr lpOutBuffer, int nOutBufferSize,
            out int lpBytesReturned, IntPtr lpOverlapped);
        public static uint CTL_CODE(uint DeviceType, uint Function, uint Method, uint Access){
            return ((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method);
        }
        #endregion
    }
}